import React, {useEffect} from "react";
import {PageLoading} from "@ant-design/pro-layout";
import {useParams} from "react-router-dom";

import {useNavigate} from "react-router-dom";
import {oauth} from "@/services/login.ts";
import {message} from "antd";

/**
 * 三方店铺授权
 * @constructor
 */
const Oauth: React.FC = () => {
    const navigate = useNavigate();
    const {platform} = useParams();

    const handleOAuth =async (platform?:string) =>{
        const {data,status} = await oauth(platform);
        console.log('---handleOAuth begin---',data,status)
        if (status === 200 && data.code === 200) {
            location.href = data.data;
            return;
        }
        console.log(data.data);
        message.error(data.msg);
        navigate(`/result/${data.code}/${data.code}/${data.msg}`, { replace: true })
    }
    useEffect(() => {
        console.log('Oauth useEffect:',platform)
        handleOAuth(platform)
    }, [platform]);
    return (
        <PageLoading tip="授权中"></PageLoading>
    )
}

export default Oauth;